package cn.lyvust.demo.shili01;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * date: 2021-5-10 20:55
 *
 * @author Guo_guo
 * @since JDK 1.8
 */
public class NewTitleDemo {
    public static void main(String[] args) {
        // 具体实现步骤
        // 1、创建多个各类新闻标题对象
        // 双斜杠是单行注释
        NewTitle car = new NewTitle(1, "汽车", "管理员");
        NewTitle test = new NewTitle(2, "高考", "管理员");

        // 2、创建存储各类新闻标题的集合对象
        List newsTitleList = new ArrayList();
        // 3、按照顺序依次添加各类新闻标题
        newsTitleList.add(car);
        newsTitleList.add(test);
        // 4、获取新闻标题的总数
        System.out.println("新闻标题数目为：" + newsTitleList.size() + "条");
        // 5、根据位置获取相应新闻标题、逐条打印每条新闻标题的名称，也就是我们常说的遍历集合对象
        System.out.println("第一种遍历ArrayList：");
        for (int i = 0; i < newsTitleList.size(); i++) {
            NewTitle title = (NewTitle) newsTitleList.get(i);
            System.out.println(i + 1 + ":" + title.getTitleName());
        }
        System.out.println("第二种遍历ArrayList：");
        for (Object obj : newsTitleList) {
            NewTitle newTitle = (NewTitle) obj;
            System.out.println(newTitle.getTitleName());
        }
        System.out.println("第三种遍历ArrayList：");
        newsTitleList.forEach(obj -> {
            NewTitle newTitle = (NewTitle)obj;
            System.out.println(newTitle.getTitleName());
        });
    }
}
